package nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

/**
 * nio Channel Demo
 *
 * Created by Zhou_Bing on 2018/9/25.
 */
public class FileCopy {

    public static void main(String[] args) throws IOException {

        String relativePath = System.getProperty("user.dir");

        FileInputStream fis = new FileInputStream(relativePath+ File.separator+"README.md");
        ReadableByteChannel source = fis.getChannel();
        FileOutputStream fos = new FileOutputStream(relativePath+File.separator+"temp.md");
        WritableByteChannel target = fos.getChannel();
        copyData(source,target);
        source.close();
        target.close();
        fis.close();
        fos.close();

    }
    public static void copyData(ReadableByteChannel src,WritableByteChannel dest) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(20*1024);
         while(src.read(buffer)!=-1){
            buffer.flip();
            while(buffer.hasRemaining()){
                dest.write(buffer);
            }
            buffer.clear();
        }
    }

}
